<?php

class OrderTypeAction extends CommonAction {

    // 查询数据
    public function index() {
        $this->display();
    }
	
	 public function indexjson() {
		$page = max(1, intval( $_POST['page'] ) );
		$rows = intval( $_POST['rows'] ) ? intval( $_POST['rows'] ) : 20;
		$limitstart = $rows*($page-1);
		$OrderType = D("OrderType");
        $list = $OrderType->limit( $limitstart , $rows)->order('id desc')->select();
		$row_tot = $OrderType->count();
       
		$i = 0;
		
		
		$i = 0;
		foreach($list AS $row ){
			$ordertype[$i]['table_id'] = $row['id'];
			$ordertype[$i]['table_name'] = $row['name'];
			$ordertype[$i]['table_info'] = $row['info'];
			$ordertype[$i]['table_status'] = $row['status'];
			$ordertype[$i]['table_sort']= $row['sort'];
			 
		
		$i++;
		}
		
		$ordertypeJSON['total'] = $row_tot;
		$ordertypeJSON['rows'] = $ordertype;
		
        echo json_encode($ordertypeJSON);
        
    }

    // 写入数据
    public function add() {
        $OrderType = D("OrderType");
        if ($vo = $OrderType->create()) {
            $list = $OrderType->add();
            if ($list !== false) {
                $this->success('数据保存成功！',U('index'));
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($OrderType->getError().D()->_sql());
        }
    }

    // 更新数据
    public function update() {
        $OrderType = D("OrderType");
        if ($vo = $OrderType->create()) {
            $list = $OrderType->save();
            if ($list !== false) {
                $this->success('数据更新成功！',U('index'));
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($OrderType->getError());
        }
    }

    // 删除数据
    public function delete() {
		$id = intval($_POST['form_id']);
		$data['status'] = 1;
        if (!empty($id)) {
            $OrderType = M("OrderType");
            $result = $OrderType->where("id='{$id}'")->save($data);
            if (false !== $result) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错！'.D()->_sql());
            }
        } else {
            $this->error('ID错误！');
        }
    }

    // 编辑数据
    public function edit($id) {
        if (!empty($id)) {
            $OrderType = M("OrderType");
            $vo = $OrderType->getById($id);
            if ($vo) {
                $this->vo   =   $vo;
                $this->display();
            } else {
                $this->error('数据不存在！');
            }
        } else {
            $this->error('数据不存在！');
        }
    }
}
?>